//
//  StatusPictureView.m
//  weibo110
//
//  Created by 臧松 on 16/1/2.
//  Copyright © 2016年 臧松. All rights reserved.
//
#define StatusPictureViewItemMargin 8
#define StatusCellMargin 12
// 可重用标示符
#define statusPictureViewCellId @"statusPictureViewCellId"

#import "StatusPictureView.h"
#import "StatusPictureCell.h"
#import "SDImageCache.h"
#import "SDWebImageManager.h"
@implementation StatusPictureView

-(void)setStatusViewModel:(StatusViewModel *)statusViewModel{
    _statusViewModel = statusViewModel;
    
    [self sizeToFit];
    
    [self reloadData];
}
-(CGSize)sizeThatFits:(CGSize)size{
    
    return [self calculorViewSize];
}

-(CGSize)calculorViewSize{
    // 每行照片的数量
    int rowCount = 3;
    
    // 最大宽度
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 2*StatusCellMargin;
    CGFloat itemWidth = (maxWidth - 2 * StatusPictureViewItemMargin)/rowCount;
    UICollectionViewFlowLayout * layout = (UICollectionViewFlowLayout*)self.collectionViewLayout;
    layout.itemSize = CGSizeMake(itemWidth, itemWidth);
    
    // 获取图片数量
    NSInteger count = self.statusViewModel.thumbnailUrls.count;
    
    // 没有图片
    if (count == 0) {
        return CGSizeZero;
    }
    // 一张图片
    if (count == 1) {
        
        CGSize size = CGSizeMake(150, 120);
        NSString * key = [self.statusViewModel.thumbnailUrls.firstObject absoluteString];
        if (key != nil) {
            
            UIImage * image =[[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:key];
            size = image.size;
        }
        // 图片过窄处理  针对长图

        size.width = size.width < 40 ? 40 : size.width;
        // 图片过宽处理
        if (size.width > 300) {
            float w = 300;
            float h = size.height * w /size.width;
            size = CGSizeMake(w, h);
        }
        //内部图片大小
        layout.itemSize = size;
        return size;
    }
    // 4张图片
    if (count == 4) {
        CGFloat w = 2 * itemWidth + StatusPictureViewItemMargin;
        return CGSizeMake(w, w);
    }
    // 其他图片
    CGFloat row = (float)((count - 1)/rowCount + 1);
    NSInteger h = row * itemWidth + (row - 1)*StatusPictureViewItemMargin;
    NSInteger w = rowCount * itemWidth + (rowCount - 1)* StatusPictureViewItemMargin;
    
    return CGSizeMake(w, h);
    
    
    return CGSizeZero;
}

- (instancetype)init
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    self = [super initWithFrame:CGRectZero collectionViewLayout:layout];
    if (self) {
        layout.minimumLineSpacing = StatusPictureViewItemMargin;
        layout.minimumInteritemSpacing = StatusPictureViewItemMargin;
        
        self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
        
        [self registerClass:[StatusPictureCell class] forCellWithReuseIdentifier:statusPictureViewCellId];
        
        self.dataSource = self;
    }
    return self;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.statusViewModel.thumbnailUrls.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
     StatusPictureCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:statusPictureViewCellId forIndexPath:indexPath];
    
    cell.imageUrl = self.statusViewModel.thumbnailUrls[indexPath.item];
    cell.backgroundColor = [UIColor lightGrayColor];
    return cell;
}
@end
